# -*- coding: UTF-8 -*-
##########################################
## 文件名称：计算hs300指标 （原手动跑的5个指标）
## 功能说明：
## 
## 
## 创建人： 
## 创建时间：
## 修改：
##
##
##########################################

import math
import os
import pickle
import pandas as pd
import numpy as np
import datetime


#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows',None)
#设置value的显示长度为100，默认为50
pd.set_option('max_colwidth',200)

##生成日期
def getDateFrame(datetime1='2005-01-01', 
                datetime2=str(datetime.date.today()-datetime.timedelta(days=1))):
    dt = datetime1
    data = []
    while dt <= datetime2:
        data.append(dt)
        temp_datetime = datetime.datetime.strptime(dt, '%Y-%m-%d')
        temp_datetime+=datetime.timedelta(days=1)
        dt = temp_datetime.strftime("%Y-%m-%d")
        
    date_df = pd.DataFrame(data, columns=["date"])
    
    date_df.set_index(['date'],inplace=True)
    return date_df

##计算环比
def ring(digit_list):
    ring_list = [None]
    for i in range(1,len(digit_list)):
        ring_list.append(1.0*digit_list[i]-digit_list[i-1])
        
    return ring_list

def get_week_day():
#     from WindPy import w
#     w.start()
#     wsd_data_pctchg = w.wsd("801010.SI,801020.SI,801030.SI,801040.SI,801050.SI,801080.SI,801110.SI,801120.SI,\
#                                   801130.SI,801140.SI,801150.SI,801160.SI,801170.SI,801180.SI,801200.SI,\
#                                   801210.SI,801230.SI,801710.SI,801720.SI,801730.SI,801740.SI,801750.SI,\
#                                   801760.SI,801770.SI,801780.SI,801790.SI,801880.SI,801890.SI", "pct_chg", 
#                                   "2005-01-01", "2020-05-26", "Period=W", usedf=True)
#  
#     day_list = wsd_data_pctchg[1].index.to_list()
#     week_day_list = [c.strftime('%Y-%m-%d') for c in day_list]
#     week_date_df = pd.DataFrame(week_day_list, columns=["date"])
#     week_date_df.set_index(['date'],inplace=True)
#     week_date_df.to_csv("week_date.csv")
    
    week_date_df = pd.read_csv('week_date.csv')
#     week_date_df = week_date_df.query("date>='2020-01-01'")
    
    week_date_list = week_date_df['date'].to_list()
    return week_date_list
    
#     week_date_df.set_index(['date'],inplace=True)
#     return week_date_df

def get_data():
    pass
    src_df = pd.read_csv('hs300_3_edb_df.csv')
    src_df=src_df.sort_values(by="date" , ascending=True)
    src_df.set_index(['date'],inplace=True)
    
    df1=src_df.loc[:,['M1001786','S0059744','M5452815','M5452823','M5452819']]
    df2=src_df.loc[:,['M0043821','M0061518']]
    
    df1 = df1.fillna(method='pad')
    df2 = df2.fillna(method='backfill')
    
    data_df = df1.join(df2,how='left')
    
    return data_df;


def factor_cal(dataDf):
    factorDf = pd.DataFrame(columns=[])
    
    ##HW_IRS1Y
    HW_IRS1Y_t1 =(dataDf['M1001786']-dataDf['S0059744']).tolist()
    factorDf['M1001786']=dataDf['M1001786']
    factorDf['S0059744']=dataDf['S0059744']
    factorDf['HW_IRS1Y_t1']=HW_IRS1Y_t1
    factorDf['HW_IRS1Y']=ring(HW_IRS1Y_t1)

      
    ##QX_Investor_Confi
    factorDf['M5452815']=dataDf['M5452815']
    QX_Investor_Confi_t1= dataDf['M5452815'].tolist()
    QX_Investor_Confi_t2 = [math.log(c) for c in QX_Investor_Confi_t1]
    QX_Investor_Confi_t3 = ring(QX_Investor_Confi_t2)
    QX_Investor_Confi_t4=[]
    for e in QX_Investor_Confi_t3:
        if e is None or e==0:
            QX_Investor_Confi_t4.append(None)
        else:
            QX_Investor_Confi_t4.append(e)
    factorDf['QX_Investor_Confi_t1']=QX_Investor_Confi_t1
    factorDf['QX_Investor_Confi_t2']=QX_Investor_Confi_t2
    factorDf['QX_Investor_Confi']=QX_Investor_Confi_t4
    
  
    ##QX_Invstor_Confi_Global,M5452823
    factorDf['M5452823']=dataDf['M5452823']
    QX_Invstor_Confi_Global_t1= dataDf['M5452823'].tolist()
    QX_Invstor_Confi_Global_t2 = [math.log(c) for c in QX_Invstor_Confi_Global_t1]
    QX_Invstor_Confi_Global_t3 = ring(QX_Invstor_Confi_Global_t2)
    QX_Invstor_Confi_Global_t4=[]
    for e in QX_Invstor_Confi_Global_t3:
        if e is None or e==0:
            QX_Invstor_Confi_Global_t4.append(None)
        else:
            QX_Invstor_Confi_Global_t4.append(e)
    factorDf['QX_Invstor_Confi_Global_t1']=QX_Invstor_Confi_Global_t1
    factorDf['QX_Invstor_Confi_Global_t2']=QX_Invstor_Confi_Global_t2
    factorDf['QX_Invstor_Confi_Global']=QX_Invstor_Confi_Global_t4
    
    factorDf['M5452823']=dataDf['M5452823']
    QX_Invstor_Confi_Global_t1= dataDf['M5452823'].tolist()
    QX_Invstor_Confi_Global_t2 = [math.log(c) for c in QX_Invstor_Confi_Global_t1]
    factorDf['QX_Invstor_Confi_Global']=ring(QX_Invstor_Confi_Global_t2)

   
#     ##QX_Invstor_Confi_GZ M5452819
#     factorDf['M5452819']=dataDf['M5452819']
#     QX_Invstor_Confi_GZ_t1= dataDf['M5452819'].tolist()
#     QX_Invstor_Confi_GZ_t2 = [math.log(c) for c in QX_Invstor_Confi_GZ_t1]
#     QX_Invstor_Confi_GZ_t3 = ring(QX_Invstor_Confi_GZ_t2)
#     QX_Invstor_Confi_GZ_t4=[]
#     for e in QX_Invstor_Confi_GZ_t3:
#         if e is None or e==0:
#             QX_Invstor_Confi_GZ_t4.append(None)
#         else:
#             QX_Invstor_Confi_GZ_t4.append(e)
#     factorDf['QX_Invstor_Confi_GZ_t1']=QX_Invstor_Confi_GZ_t1
#     factorDf['QX_Invstor_Confi_GZ_t2']=QX_Invstor_Confi_GZ_t2
#     factorDf['QX_Invstor_Confi_GZ']=QX_Invstor_Confi_GZ_t4

   
    ##Ted利差    HG_Rd edb: M0043821,M0061518    
    factorDf['M0061518']=dataDf['M0061518']
    factorDf['M0043821']=dataDf['M0043821']
    HG_Rd_t1= (dataDf['M0061518']+dataDf['M0043821']).tolist()
    factorDf['HG_Rd']=ring(HG_Rd_t1)
     
      
    factorDf=factorDf.fillna(method='pad') ##填补缺失值  
    factorDf.index=dataDf.index
    return factorDf
    


if __name__ == '__main__':
    pass

    week_date_list=get_week_day()
#     print(week_date_list)
      
    src_df = get_data()
       
    date_df =getDateFrame()
    data_df = date_df.join(src_df,how='left')
       
    data_df=data_df.fillna(method='pad') ##填补缺失值backfill/bfill
    data_df = data_df.query("date>='2009-01-01'")
    data_df.to_csv("data_df.csv") ##week_date_list
    week_data_df = data_df[data_df.index.isin(week_date_list)]
         
    week_data_df.to_csv("week_data_df.csv")
     
#     print("week_data_df:\n",week_data_df)
    factorDf = factor_cal(week_data_df)
    factorDf = factorDf.query("date>'2010-01-01'")
    factorDf.to_csv("factor.csv")
       
#     week_day = get_week_day()
#     print(week_day)
#     week_factorDf=factorDf.loc[factorDf.date.isin(week_day),:]
# #     week_factorDf = factorDf[factorDf['date'] in week_day]
#     print(week_factorDf) 
     
    